package com.cyys.modules.system.controller.param;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.system.model.param.ParamBasic;
import com.cyys.modules.system.service.param.ParamBasicService;
import com.cyys.modules.system.service.param.ParamBasicTypeService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/paramBasic")
public class ParamBasicAction extends BaseAction {

	@Resource
	private ParamBasicService paramBasicService;

	@Resource
	private ParamBasicTypeService paramBasicTypeService;

	@GetMapping("/add/input")
	@RequiresPermissions(value = "sys:paramBasic:add")
	public String input()  {
		return "module/system/param/paramBasic-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "sys:paramBasic:add")
	@SystemLog(module = "系统参数", methods = "基础参数-保存基础参数大类")
	public AR save(@Validated(Insert.class)ParamBasic paramBasic)  {
		return paramBasicService.save(paramBasic);
	}

	@GetMapping("/edit/input")
	@RequiresPermissions(value = "sys:paramBasic:edit")
	public String editInput(ParamBasic paramBasic, Model model)  {
		model.addAttribute("paramBasic", paramBasicService.getById(paramBasic.getId()));
		return "module/system/param/paramBasic-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "sys:paramBasic:edit")
	@SystemLog(module = "系统参数", methods = "基础参数-更新基础参数大类")
	public AR edit(ParamBasic paramBasic, String oldId)  {
		return paramBasicService.update(paramBasic, oldId);
	}

	@GetMapping("/list")
	@RequiresPermissions("sys:paramBasic:list")
	public String list(Model model)  {
		return "module/system/param/paramBasic-list";
	}

	@ResponseBody
	@PostMapping("/init/initParamBasic")
	@RequiresPermissions("sys:paramBasic:init")
	@SystemLog(module = "系统参数", methods = "基础参数-更新基础参数大类缓存")
	public AR initParamBasic(Model model)  {
		ParamBasicUtil.initParam(paramBasicService, paramBasicTypeService);
		return AR.ok("更新缓存成功！");
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("sys:paramBasic:del")
	@SystemLog(module = "系统参数", methods = "基础参数-删除基础参数大类")
	public AR deleteById(String id)  {
		return paramBasicService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("sys:paramBasic:del")
	@SystemLog(module = "系统参数", methods = "基础参数-批量删除基础参数大类")
	public AR deleteByIds(String ids)  {
		return paramBasicService.deleteBatchIds(ids);
	}

	@ResponseBody
	@PostMapping("/list/paramBasicList")
	@RequiresPermissions("sys:paramBasic:list")
	public String paramBasiclist(ParamBasic paramBasic, Page page)  {
		QueryWrapper qw = new QueryWrapper(paramBasic);
		qw.orderByAsc("sort");
		return JSONData.toLayerData(paramBasicService.list(page , qw,false));
	}

	@ResponseBody
	@PostMapping("/add/checkId")
	@RequiresPermissions(value = {"sys:paramBasic:add", "sys:paramBasic:edit"},logical=Logical.OR)
	public AR checkId(String id)  {
		if (id != null) {
			ParamBasic paramBasic = paramBasicService.getById(id);
			if (paramBasic != null) {
				return AR.failed("id已存在！");
			} else {
				return AR.ok("id不存在！");
			}
		} else {
			return AR.failed("id为空，请重新传值！！");
		}
	}
}
